package exchange;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Scanner;

public class Solution {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		try {
			int K = in.nextInt(), P[] = new int[K];
			for(int k = 0; k < K; k++)
				P[k] = in.nextInt();
			Map<Integer, List<Integer>> swap = new HashMap<Integer, List<Integer>>();
			in.nextLine(); // take new line after last integer scanned
			for(int k = 0; k < K; k++) {
				char[] c = in.nextLine().toCharArray();
				for(int i = 0; i < K; i++) {
					if(c[i] != 'Y')
						continue;
					List<Integer> l = swap.get(k);
					if(l == null) {
						l = new ArrayList<Integer>();
						swap.put(k, l);
					}
					l.add(i);
				}
			}
			for(int k = 0; k < K; k++) {
				Queue<Integer> q = new LinkedList<Integer>();
				boolean visited[] = new boolean[K];
				q.add(k);
				int minInd = k;
				while(!q.isEmpty()) {
					int ind = q.remove();
					if(visited[ind])
						continue;
					if(P[ind] < P[minInd])
						minInd = ind;
					visited[ind] = true;
					List<Integer> l = swap.get(ind);
					if(l == null)
						continue;
					for(int item : l)
						q.add(item);
				}
				if(k > 0)
					System.out.print(" ");
				System.out.print(P[minInd]);
				P[minInd] = Integer.MAX_VALUE;
			}
			System.out.println();
		} finally {
			in.close();
		}
	}
}
